<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">




          <a-col :span="8">
            <a-form-model-item label="档案大类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveMajorCategory">
              <j-dict-select-category :disabled=formDisabled v-model="model.archiveMajorCategory" placeholder="请选择费用大类" parentCode="A04" removeCode="A04A02,A04A01" :async="true" disabled/>
            </a-form-model-item>
          </a-col>

          <a-col :span="8">
            <a-form-model-item label="档案编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileNo">
              <a-input  v-model="model.fileNo" readOnly @click.prevent="goBillDetail(model.billId)" style="color: #1890FF; cursor: pointer;" :max-length="36"  placeholder="请输入档案编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectNo">
              <a-input :disabled=formDisabled v-model="model.projectNo" :max-length="36"  placeholder="请输入项目编号"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <a-input  readOnly @click.prevent="onDetail(model.projectId)"  v-model="model.projectName" style="color: #1890FF; cursor: pointer;"  placeholder="请选择项目名称"></a-input>
<!--              <select-project-input v-else ref="projectInput" :disabled="formDisabled" v-model="model.projectId" :proDisabled="true" :allowClear="false" placeholder="请输入选择项目" :projectStatus="'2'" @input="projectChangeHandle" disabled />-->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="签订单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signUnit">
              <select-Company-input :disabled="formDisabled" v-model="model.signUnit" placeholder="请选择签订单位" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="档案所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveBelongingTeam">
              <select-team-input
                v-model="model.archiveBelongingTeam"
                :tdlx="1"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="业务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessName">
              <select-task-file-for-project-archive-input :disabled=formDisabled v-model="model.businessName" :allowClear="false" :projectId="model.projectId" :isShowCategoryName="true" :isExcludeWaitingStatus="true"  disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="档案小类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveMajorCategory">
              <j-dict-select-category :disabled="formDisabled || !model.archiveMajorCategory" v-model="model.archiveMinorCategory" placeholder="请选择费用小类" :parentCode="model.archiveMajorCategory" :async="true" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectLeading">
              <select-user-by-dep
                v-model="model.projectLeader"
                disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="密级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="secrecyLevel">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.secrecyLevel" dictCode="secrecy_level" placeholder="请选择密级" disabled/>
            </a-form-model-item>
          </a-col>






          <a-col :span="8">
            <a-form-model-item label="存档时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveTime">
              <j-date :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="请选择存档时间"
                      v-model="model.archiveTime"
                      style="width: 100%"
                      :disabled=formDisabled
              />
<!--              <j-date :disabled=formDisabled placeholder="请选择存档时间" v-model="model.archiveTime"  style="width: 100%" />-->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="档案闭合时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileCloseTime">
              <j-date :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="请选择档案闭合时间"
                      v-model="model.fileCloseTime"
                      style="width: 100%"
                      :disabled=formDisabled
              />
<!--              <j-date :disabled=formDisabled placeholder="请选择档案闭合时间" v-model="model.fileCloseTime"  style="width: 100%" />-->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="存档人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archivePerson">
              <select-user-by-dep   :multi="false" v-model="model.archivePerson" :disabled=formDisabled></select-user-by-dep>
<!--              <a-input :disabled=formDisabled v-model="model.archivePerson" :max-length="36"  placeholder="请输入存档人"  ></a-input>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="接收人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiver">
              <select-user-by-dep   :multi="false" v-model="model.receiver" :disabled=formDisabled></select-user-by-dep>
<!--              <a-input :disabled=formDisabled v-model="model.receiver" :max-length="36"  placeholder="请输入接收人"  ></a-input>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="存档位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="archiveLocation">
              <a-input  v-model="model.archiveLocation" :max-length="36"  placeholder="请输入存档位置" :disabled=formDisabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-textarea :maxLength="500" :disabled="formDisabled" v-model="model.remarks" rows="4" placeholder="请输入备注" />
<!--              <a-input :disabled=formDisabled v-model="model.remarks" :max-length="36"  placeholder="请输入备注"  ></a-input>-->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>

    <!-- 项目详情开始 -->
    <erp-project-report-modal  ref="projectModalForm"></erp-project-report-modal>
    <!-- 项目详情结束 -->

    <a-modal
      :maskClosable="false"
      title="项目存档申请单详情"
      :width="1200"
      :bodyStyle="{
            padding: '0 24px'
          }"
      :visible="visibleDetail"
      @cancel="handleCancelDetail"
      cancelText="关闭">
      <erp-project-archive-form ref="realForm" :disabled="true" normal />
      <template slot="footer">
        <a-button @click.stop="handleCancelDetail">关闭</a-button>
      </template>
    </a-modal>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import JDictSelectCategory from '@/components/dict/JDictSelectCategory.vue'
  import SelectTaskFileForProjectArchiveInput from '@/views/scy/components/SelectTaskFileForProjectArchiveInput'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  import ErpProjectReportModal from '@/views/erp/report/project/modules/ErpProjectReportModal'
  import moment from 'moment'
  import { mapGetters } from 'vuex'
  import ErpProjectArchiveForm from './ErpProjectArchiveForm.vue';

  export default {
    name: 'ErpProjectArchiveFileForm',
    components: {
      ErpProjectArchiveForm,
      SelectUserByDep,
      SelectTeamInput,
      SelectCompanyInput,
      SelectCustomerInput,
      JDictSelectCategory,
      SelectTaskFileForProjectArchiveInput,
      SelectProjectInput,
      ErpProjectReportModal
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        visibleDetail: false,
        validatorRules: {
          archiveTime: [
            { required: true, message: '请选择存档时间!' }
          ],

          fileCloseTime: [
            { required: true, message: '请选择档案闭合时间!' }
          ],
          archivePerson: [
            { required: true, message: '请选择存档人!' }
          ],
          receiver: [
            { required: true, message: '请选择接收人!' }
          ],
          archiveLocation: [
            { required: true, message: '请输入存档位置!'}
          ],
        },
        url: {
          add: "/api/erp/omc/projectarchive/erpProjectArchiveFile/add",
          edit: "/api/erp/omc/projectarchive/erpProjectArchiveFile/edit",
          queryById: "/api/erp/omc/projectarchive/erpProjectArchiveFile/queryById",
          queryProjectById: '/api/erp/project/report/erpProjectReport/queryById',
          queryProjectArchive: "/api/erp/omc/projectarchive/erpProjectArchive/queryById",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      onDetail(id) {
        const that = this
        this.$nextTick(() => {
          getAction(this.url.queryProjectById, { id }).then((res) => {
            if (res.success) {
              const model = res.result
              that.$refs.projectModalForm.getDetail(model);
              that.$refs.projectModalForm.title = '项目一览表';
            }
          })
        })
      },
      goBillDetail(id){
        this.$nextTick(()=>{
          getAction(this.url.queryProjectArchive, { id }).then((res) => {
            if (res.success) {
              this.visibleDetail = true
              const model = res.result
              this.$nextTick(()=>{
                this.$refs.realForm.edit(model);
              });
            }
          })
        });
      },
      handleCancelDetail(){
        this.visibleDetail = false
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error)=>{
               that.$emit('unButtonDisable');
             }).finally(() => {
               that.confirmLoading = false;
             })
          }else {
           that.$emit('unButtonDisable');
          }

        })
      },
    }
  }
</script>
